/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WorkSpacePanel.java
 *
 * Created on Jul 12, 2012, 10:40:41 AM
 */

package com.piza.toolcenter.view;

import com.piza.toolcenter.operation.IWorkFlow;
import com.piza.toolcenter.operation.SessionContext;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;

/**
 *
 * @author cwb
 */
public class WorkSpacePanel extends javax.swing.JPanel {

    
    private SessionContext context;
    private int currentStep=1;
    private Map<Integer,IWorkFlow> workFlow=new HashMap<Integer, IWorkFlow>();
    
    /** Creates new form WorkSpacePanel */
    public WorkSpacePanel() {
        initComponents();
    }

    public void setWorkFlow(Map<Integer,IWorkFlow> workFlow){
        this.workFlow=workFlow;
    }
    
    public void setContext(SessionContext context){
        this.context=context;
    }

    private JPanel getWorkFlowPanel(int i){
        JPanel panel=(JPanel)this.workFlow.get(new Integer(i));
        return panel;
    }
    public void start(){
        JPanel startPanel=getWorkFlowPanel(currentStep);
        if(startPanel==null){
            return;
        }
        changePanel(startPanel);
        if(startPanel instanceof IWorkFlow){
              ((IWorkFlow)startPanel).open(context);
          }
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        previousButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        workingPanel = new javax.swing.JPanel();

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        previousButton.setText("Previous");
        previousButton.setName("previousButton"); // NOI18N
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        nextButton.setText("Next");
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        okButton.setText("Ok");
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(427, Short.MAX_VALUE)
                .addComponent(previousButton)
                .addGap(26, 26, 26)
                .addComponent(nextButton)
                .addGap(29, 29, 29)
                .addComponent(okButton)
                .addGap(101, 101, 101))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(previousButton)
                    .addComponent(nextButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        workingPanel.setName("workingPanel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(workingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(workingPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        
        if(!this.workFlow.get(currentStep).close()){
           return; 
        }
        JPanel nextPanel=getWorkFlowPanel(++currentStep);
        if(nextPanel!=null){
            changePanel(nextPanel);
            if(nextPanel instanceof IWorkFlow){
              ((IWorkFlow)nextPanel).open(context);
          }
        }
        
    }//GEN-LAST:event_nextButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed

        JPanel prePanel=getWorkFlowPanel(--currentStep);
        if(prePanel!=null){
            changePanel(prePanel);
            
        }
    }//GEN-LAST:event_previousButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

        this.context.clean();
        
    }//GEN-LAST:event_okButtonActionPerformed

    private void changePanel(JPanel panel){
         this.workingPanel.removeAll();
          this.workingPanel.add(panel);
          this.workingPanel.validate();
          this.workingPanel.repaint();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton okButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JPanel workingPanel;
    // End of variables declaration//GEN-END:variables

}
